<?php
namespace app\index\controller\user;

use app\common\model\paths\ClassModel;
use app\common\model\paths\TaskChapterModel;
use app\common\model\paths\TaskModel;
use app\common\model\paths\UserStudyGoldModel;
use app\index\controller\BaseIndex;
use app\paths\controller\Task;
use think\admin\model\SystemUser;

/**
 * 我的成绩
 * @package app\index\controller\user
 * @author wangqinghui
 * @DateTime 2024-09-30 09:45:49
 */
class Achievement extends BaseIndex
{
    // 是否强制登录
    protected $need_login = true;

    /**
     * 成绩列表
     * @author wangqinghui
     * @DateTime 2024-09-30 09:47:50
     * @return void
     */
    public function index()
    {
        $this->title = lang('我的成绩');
        $this->fetch('index');
    }

    /**
     * 获取成绩列表
     * @author wangqinghui
     * @DateTime 2024-09-30 09:49:03
     * @return void
     */
    public function getList()
    {
        // 获取我的加入班级
        $map = [
            ['index_user_id',   '=',    $this->user_id],
        ];
        // 分页
        $page = (int)$this->request->post('page', 1);
        $limit = (int)$this->request->post('limit', 10);
        $field = 'class_id,task_id,sum(task_gold) total_task_gold,sum(employ_gold) total_employ_gold,sum(total_gold) total_total_gold';
        $total = UserStudyGoldModel::mk()->field($field)->where($map)->group('task_id,class_id')->count();
        $list = UserStudyGoldModel::mk()->field($field)->where($map)->group('task_id,class_id')->order('class_id asc')->page($page, $limit)->select()->toArray();

        // 获取课程信息
        $course_ids = array_column($list, 'task_id');
        $course_data = $teacher_data = [];
        if(!empty($course_ids)){
            $course_data = TaskModel::mk()->whereIn('id', $course_ids)->column('title,user_id', 'id');
            // 获取课程所属老师
            $teacher_ids = array_column($course_data, 'user_id');
            if(!empty($teacher_ids)){
                $teacher_data = SystemUser::mk()->whereIn('id', $teacher_ids)->column('nickname', 'id');
            }
        }
        // 获取班级信息
        $class_ids = array_column($list, 'class_id');
        $class_data = [];
        if(!empty($class_ids)){
            $class_data = ClassModel::mk()->whereIn('id', $class_ids)->column('name', 'id');
        }
        // 获取课程总分数
        $map = [
            ['tc.task_id',     'in',    $course_ids],
            ['tc.status',      '=',    1],
        ];
        $field = 'tc.task_id,sum(score) as total_score,sum(gold) as total_gold';
        $task_score_list = TaskChapterModel::mk()->alias('tc')->field($field)->where($map)->leftJoin('paths_employ_info ei', 'tc.employ_id = ei.app_id and ei.level = 1 and ei.status = 1')->group('tc.task_id')->select()->toArray();
        $task_score_data = array_column($task_score_list, null, 'task_id');

        if(!empty($list)){
            // 处理数据
            foreach($list as $k => $v){
                $course_info = !empty($course_data[$v['task_id']]) ? $course_data[$v['task_id']] : [];
                $list[$k]['course_name'] = $course_info['title'] ?? '';
                $list[$k]['teacher_name'] = !empty($teacher_data[$course_info['user_id']]) ? $teacher_data[$course_info['user_id']] : '';
                $list[$k]['class_name'] = $class_data[$v['class_id']] ?? '';
                $list[$k]['total_task_score'] = !empty($task_score_data[$v['task_id']]) ? ($task_score_data[$v['task_id']]['total_score'] + $task_score_data[$v['task_id']]['total_gold']) : 0;
            }
        }
        $this->success('获取成功', ['total' => $total, 'items' => $list], 0);
    }

    /**
     * 获取课程成绩明细
     * @author wangqinghui
     * @DateTime 2024-09-30 15:24:39
     * @return void
     */
    public function detail()
    {
        $task_id = (int)$this->request->post('task_id', 0);
        $class_id = (int)$this->request->post('cl_id', 0);
        if(empty($task_id) || ($task_id <= 0)){
            $this->error('课程信息有误', [], 1);
        }
        if(empty($class_id) || ($class_id <= 0)){
            $this->error('班级信息有误', [], 1);
        }

        // 获取班级信息
        $map = [
            ['id',      '=',    $class_id],
        ];
        $class_info = ClassModel::mk()->field('id,name')->where($map)->find();
        if(empty($class_info)){
            $this->error('班级信息有误', [], 1);
        }

        // 分页
        $page = (int)$this->request->post('page', 1);
        $limit = (int)$this->request->post('limit', 10);
        // 获取成绩详情
        $map = [
            ['task_id',         '=',    $task_id],
            ['index_user_id',   '=',    $this->user_id],
            ['class_id',      '=',    $class_id]
        ];
        $total = UserStudyGoldModel::mk()->where($map)->count();
        $items = UserStudyGoldModel::mk()->where($map)->page($page, $limit)->select()->toArray();

        // 获取章节信息
        $map = [
            ['task_id',     '=',    $task_id],
            ['status',      '=',    1],
        ];
        $chapter_list = TaskChapterModel::mk()->where($map)->column('id,title,pid', 'id');
        if(!empty($items)){
            foreach ($items as $key => $value) {
                $items[$key]['chapter_name'] = !empty($chapter_list[$value['task_chapter_id']]['title']) ? $chapter_list[$value['task_chapter_id']]['title'] : '';
                $items[$key]['chapter_pid_name'] = !empty($chapter_list[$value['task_chapter_pid']]['title']) ? $chapter_list[$value['task_chapter_pid']]['title'] : '';
            }
        }
        $this->success('获取成功', ['total' => $total, 'items' => $items, 'class_info' => $class_info->toArray()], 0);
    }
}